package com.sen.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sen.entity.ActivityEntity;
import com.sen.entity.UserEntity;
import com.sen.service.UserService;
import com.sen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 用户表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-08 17:54:40
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页查询所有用户
     * @param user 用户实体类
     * @return
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String,String> user){
        Page<UserEntity> page = new Page<>(Integer.parseInt(user.get("current")),Integer.parseInt(user.get("size")));
        IPage<UserEntity> users = userService.queryAllUser(user,page);
        return R.ok().put("users",users);
    }
    /**
     * 增加或修改用户信息
     * @param userEntity 用户实体类
     * @return
     */
    @RequestMapping("/userSaveOrUpd")
    public R userSaveOrUpd(UserEntity userEntity){
        userService.saveOrUpdate(userEntity);
        return R.ok();
    }
    @RequestMapping("/del")
    public R del(String uid){
        userService.removeById(uid);
        return R.ok();
    }
    @RequestMapping("/getPhonr")
    public R getPhonr(String phone){
        UserEntity user = userService.getOne(new QueryWrapper<UserEntity>().eq("phone", phone));
        if (user!=null){
            return R.error().put("user",user);
        }
        else{
            return R.ok();
        }
    }
}
